package s47.dqj.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import s47.dqj.bean.PatientReport;

/**
 * 20.患者病历表
 * @author Administrator
 *
 */
@Repository("patientReportDaoImpl")
public class PatientReportDaoImpl extends HibernateDaoSupport implements PatientReportDaoInf{
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#setSupperSessionFactory(org.hibernate.SessionFactory)
	 */
	@Override
	@Resource(name="sessionFactory")
	public void setSupperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#findByPidAndPrdate(s47.dqj.bean.PatientReport)
	 */
	@Override
	public List<PatientReport> findByPidAndPrdate(PatientReport patientReport){
		String hql="from PatientReport as pr where pr.pid.pid=:pid order by pr.prdate";
		Query query=getSession().createQuery(hql);
		query.setString("pid", patientReport.getPid().getPid());
		query.setFirstResult(0);
		query.setMaxResults(5);
		return query.list();	
	}
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#findByPidAndDpid(s47.dqj.bean.PatientReport, java.lang.Integer)
	 */
	@Override
	public List<PatientReport> findByPidAndDpid(PatientReport patientReport,Integer pageNum){
		String hql="from PatientReport as pr where pr.pid.pid=:pid and pr.dpid.dpid=:dpid";
		Query query=getSession().createQuery(hql);
		query.setString("pid", patientReport.getPid().getPid());
		query.setInteger("pid", patientReport.getDpid().getDpid());
		query.setFirstResult((Integer.valueOf(pageNum)-1)*15);
		query.setMaxResults(15);
		return query.list();
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#selectPageTotalByPidAndDpid(s47.dqj.bean.PatientReport, java.lang.Integer)
	 */
	@Override
	public List<Integer> selectPageTotalByPidAndDpid(PatientReport patientReport,Integer pageNum){
		List<Integer> pageTotal=new ArrayList<Integer>();
		int p;
		Long temp;
		String hql="select Count(*) from PatientReport as pr where pr.pid.pid=:pid and pr.dpid.dpid=:dpid";
		Query query=getSession().createQuery(hql);
		query.setString("pid", patientReport.getPid().getPid());
		query.setInteger("pid", patientReport.getDpid().getDpid());
		temp=(Long)query.uniqueResult();
		
		if(temp%15!=0)
			p=(int)(temp/15+1);
		else
			p=(int)(temp/15);
		for(int n=0;n<p;n++){
			pageTotal.add(n+1);
		}
		return pageTotal;
	} 
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#findByPrdiagnose(s47.dqj.bean.PatientReport, java.lang.Integer)
	 */
	@Override
	public List<PatientReport> findByPrdiagnose(PatientReport patientReport,Integer pageNum){
		String hql="from PatientReport as pr where pr.prdiagnose='%"+patientReport.getPrdiagnose()+"%'";
		Query query= getSession().createQuery(hql);
		query.setFirstResult((Integer.valueOf(pageNum)-1)*15);
		query.setMaxResults(15);
		return query.list();
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#selectPageTotalByPrdiagnose(s47.dqj.bean.PatientReport, java.lang.Integer)
	 */
	@Override
	public List<Integer> selectPageTotalByPrdiagnose(PatientReport patientReport,Integer pageNum){
		List<Integer> pageTotal=new ArrayList<Integer>();
		int p;
		Long temp;
		String hql="select Count(*) from PatientReport as pr where pr.prdiagnose='%"+patientReport.getPrdiagnose()+"%'";
		Query query=getSession().createQuery(hql);
		temp=(Long)query.uniqueResult();
		
		if(temp%15!=0)
			p=(int)(temp/15+1);
		else
			p=(int)(temp/15);
		for(int n=0;n<p;n++){
			pageTotal.add(n+1);
		}
		return pageTotal;
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#findAllByCondition(java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<PatientReport> findAllByCondition(String hql,Integer pageNum){
		Query query=getSession().createQuery(hql);
		query.setFirstResult((Integer.valueOf(pageNum)-1)*20);
		query.setMaxResults(20);
		return query.list();
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#selectPageTotal(java.lang.String)
	 */
	@Override
	public List<Integer> selectPageTotal(String tempHQL){
		List<Integer> pageTotal=new ArrayList<Integer>();
		int p;
		Long temp;
		String hql="select Count(*) from PatientReport as pr"+tempHQL;
		Query query=getSession().createQuery(hql);
		temp=(Long)query.uniqueResult();
		
		if(temp%20!=0)
			p=(int)(temp/20+1);
		else
			p=(int)(temp/20);
		for(int n=0;n<p;n++){
			pageTotal.add(n+1);
		}
		return pageTotal;
	} 
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#save(s47.dqj.bean.PatientReport)
	 */
	@Override
	public void save(PatientReport patientReport){
		getHibernateTemplate().save(patientReport);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#update(s47.dqj.bean.PatientReport)
	 */
	@Override
	public void update(PatientReport patientReport){
		getHibernateTemplate().update(patientReport);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#delete(s47.dqj.bean.PatientReport)
	 */
	@Override
	public void delete(PatientReport patientReport){
		getHibernateTemplate().delete(patientReport);
	}
	/* (non-Javadoc)
	 * @see s47.dqj.dao.PatientReportDaoInf#findObject(s47.dqj.bean.PatientReport)
	 */
	@Override
	public PatientReport findObject(PatientReport patientReport){
		return getHibernateTemplate().get(PatientReport.class, patientReport.getPrid());
	}
	
	
}
